import { login, logout, getInfo, loginGitee } from '@/api/user'
import { getRouters } from '@/api/menu'
import { getToken, setToken, removeToken, removeXToken, setXToken, getXToken } from '@/utils/auth'
import { constantRoutes } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    xtoken: getXToken(),
    name: '',
    avatar: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_XTOKEN: (state, xtoken) => {
    state.xtoken = xtoken
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_MENU: (state, menus) => {
    state.menus = menus
  },
  SET_ROUTES: (state, routes) => {
    state.routes = routes
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({
        username: username.trim(),
        password: password,
        grant_type: 'password',
        client_id: 'client',
        client_secret: '123456'
      }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.access_token)
        setToken(data.access_token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  loginGitee({ commit }) {
    return new Promise((resolve, reject) => {
      loginGitee().then(response => {
        console.log(response)
        commit('SET_XTOKEN', response.data)
        setXToken(response.data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const { username, avatar } = data
        commit('SET_NAME', username)
        commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  getMenu({ commit, state }) {
    return new Promise((resolve, reject) => {
      getRouters(state.token).then(response => {
        const { data } = response
        if (!data) {
          return reject('没有找到任何菜单')
        }
        const menus = data
        const routes = constantRoutes.concat(menus)
        commit('SET_ROUTES', routes)
        commit('SET_MENU', menus)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        commit('SET_MENU', '')
        removeToken() // must remove  token  first
        removeXToken()
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      removeXToken()
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

